package mongo

import (
	"cgs-server/helper"
	"cgs-server/server"
	"cgs-server/server/cgs/app/models"
	v1 "cgs-server/server/cgs/app/models/v1"
	"cgs-server/server/cgs/app/store"
	"cgs-server/server/model"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

type publicWorksheetExt struct {
	db             *helper.Mongo
	collectionName string
}

func (w *publicWorksheetExt) List(wg *models.ExtUrl, user *model.User, opts v1.ListOptions) (*models.ExtUrls, error) {
	filter := opts.GetFilter()
	results := &models.ExtUrls{}
	err := w.db.FindMany(w.collectionName, filter, results, opts.ResolveFindOptions())
	return results, err
}

func (w *publicWorksheetExt) Save(wg *models.ExtUrl, user *model.User, opts v1.CreateOptions) error {
	wg.SetId(primitive.NewObjectID())
	_, err := w.db.InsertOne(w.collectionName, wg)
	return err
}

func (w *publicWorksheetExt) Delete(wg *models.ExtUrl, user *model.User, opts v1.DeleteOptions) error {
	filter := opts.GetFilter()
	if !wg.Id.IsZero() {
		filter["id"] = wg.Id
	}
	_, err := w.db.DeleteOne(w.collectionName, filter)
	return err
}

func newPublicWorksheetExtStore(d *datastore) store.PublicWorksheetExtStore {
	return &publicWorksheetExt{
		db:             d.db,
		collectionName: server.WorksheetPublicExtCollectionName,
	}
}
